#include "lvgl/lvgl.h"
#include "lvgl/demos/lv_demos.h"
#include "lv_drivers/display/fbdev.h"
#include "lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/calculator.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/album.h"
#include "./../../inc/apps.h"
#include "./../../inc/setting.h"
#include "./../../inc/music.h"
#include "./../../inc/tic_tac_toe.h"
#include "./../../inc/2048.h"
#include "./../../inc/draw.h"

void open_calculator(lv_event_t * e)
{
    ui_mode             = WHITE_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    calculator_init();
}

void open_draw(lv_event_t * e)
{
    lv_obj_t * draw_screen = lv_obj_create(NULL);
    navigate_to(draw_screen);
    ui_mode             = WHITE_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    draw_init();
    detect_init();
    if(history_index > 0) {
        // 隐藏当前页面
        lv_obj_add_flag(page_history[history_index], LV_OBJ_FLAG_HIDDEN);

        // 显示上一个页面
        history_index--;
        lv_obj_clear_flag(page_history[history_index], LV_OBJ_FLAG_HIDDEN);
        lv_scr_load(page_history[history_index]);
    }
}

void open_album(lv_event_t * e)
{
    ui_mode             = SHOW_ALL_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    album_init();
}

void open_apps(lv_event_t * e)
{
    ui_mode             = SHOW_ALL_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    apps_init();
}

void open_setting(lv_event_t * e)
{
    ui_mode             = HIDE_AVATAR_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    setting_mode        = SETTING_MODE_ACCOUNT;
    setting_init();
}

void open_music(lv_event_t * e)
{
    ui_mode             = SHOW_ALL_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = WHITE_MODE;
    labal_color         = WHITE_MODE;
    music_init();
}

void open_login(lv_event_t * e)
{
    lv_obj_t * setting_scr = lv_event_get_user_data(e);
    login_init(setting_scr);
}

void open_tic_tac_toe(lv_event_t * e)
{
    ui_mode             = HIDE_AVATAR_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = BLACK_MODE;
    labal_color         = BLACK_MODE;
    tic_tac_toe_init();
    if(tic_tac_toe_screen != NULL) {
        restart_game();
    }
}

void open_2048(lv_event_t * e)
{
    ui_mode             = HIDE_AVATAR_MODE;
    ui_taskbar_color    = WHITE_MODE;
    ui_status_bar_color = BLACK_MODE;
    labal_color         = BLACK_MODE;
    game_2048_init();
    // if(game_2048_screen != NULL) {
    //     restart_game();
    // }
}